﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;

namespace NotAClue.Web.DynamicData
{
    public class ChildChangingAwareFieldTemplate : FieldTemplateUserControl
    {
        /// <summary>
        /// Gets or sets the parent column.
        /// </summary>
        /// <value>The parent column.</value>
        public Dictionary<String, MetaColumn> ParentColumns { get; set; }

        /// <summary>
        /// Gets or sets the parent control.
        /// </summary>
        /// <value>The parent control.</value>
        public Dictionary<String, IChangeNotifyingFieldTemplate> ParentControls { get; set; }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            ParentColumns = new Dictionary<String, MetaColumn>();
            ParentControls = new Dictionary<String, IChangeNotifyingFieldTemplate>();

            // get the parent column
            var attributes = Column.Attributes.OfType<ChangeAwareAttribute>();
            foreach (var attribute in attributes)
            {
                var parentColumn = attribute.ParentColumn;
                if (!String.IsNullOrEmpty(parentColumn))
                {
                    try
                    {
                        var column = Column.Table.GetColumn(parentColumn) as MetaColumn;
                        ParentColumns.Add(attribute.ParentColumn, column);
                    }
                    catch (InvalidOperationException)
                    {
                        ParentColumns = null;
                    }
                }
                if (ParentColumns != null)
                {
                    var parentControl = GetParentControl(ParentColumns[parentColumn]);
                    ParentControls.Add(parentColumn, parentControl);
                }
            }
            // finally call base
            base.OnInit(e);
        }

        /// <summary>
        /// Gets the Parent control in a cascade of controls
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        private IChangeNotifyingFieldTemplate GetParentControl(MetaColumn parentColumn)
        {
            if (parentColumn != null)
            {
                // get parent control 
                var parentDataControl = this.GetContainerControl<DataBoundControl>();
                DynamicControl parentDynamicControl;

                if (parentDataControl is ListView)
                {
                    var parentRow = this.GetContainerControl<HtmlTableRow>();
                    // Get Parent FieldTemplate
                    parentDynamicControl = parentRow.FindDynamicControlRecursive(parentColumn.Name);
                }
                else
                {
                    // Get Parent FieldTemplate
                    parentDynamicControl = parentDataControl.FindDynamicControlRecursive(parentColumn.Name);
                }
                // extract the parent control from the DynamicControl
                IChangeNotifyingFieldTemplate parentControl = null;
                if (parentDynamicControl != null && parentDynamicControl.Controls.Count > 0)
                {
                    var a = parentDynamicControl.Controls[0];
                    parentControl = parentDynamicControl.Controls[0] as IChangeNotifyingFieldTemplate;
                }

                return parentControl;
            }
            return null;
        }
    }
}